package com.htu.service.Impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.htu.service.AliPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
@Service
public class AliPayServiceImpl implements AliPayService {
    @Autowired
    AlipayClient alipayClient;
    @Override
    public Map createNative(String out_trade_no, String total_fee) {
     Map <String,String> map = new HashMap<>();
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        long total = Long.parseLong(total_fee);
        BigDecimal bigYuan = BigDecimal.valueOf(total);
        System.out.println("支付金额："+bigYuan.doubleValue());
        request.setBizContent("{" +
                "    \"out_trade_no\":\""+out_trade_no+"\"," +
                "    \"total_amount\":\""+bigYuan.doubleValue()+"\"," +
                "    \"subject\":\"测试购买商品001\"," +
                "    \"store_id\":\"xx_001\"," +
                "    \"timeout_express\":\"120m\"}");

        try {
            AlipayTradePrecreateResponse response = alipayClient.execute(request);
            String code = response.getCode();
            System.out.println("response:"+code);
            String body = response.getBody();
            System.out.println("response body:"+body);
            if (code.equals("10000")){
                map.put("qrcode",response.getQrCode());
                map.put("out_trade_no",response.getOutTradeNo());
                map.put("total_fee",total_fee);
                System.out.println("qrcode"+response.getQrCode());
                System.out.println("out_trade_no"+response.getOutTradeNo());
                System.out.println("total_fee"+total_fee);
            }else {
                System.out.println("error"+body);
            }

        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return map;

    }

    @Override
    public Map queryPayStatus(String out_trade_no) {
        Map<String,String> map = new HashMap<>();
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.setBizContent("{" +
                "    \"out_trade_no\":\""+out_trade_no+"\"," +
                "    \"trade_no\":\"\"}");
        try {
            AlipayTradeQueryResponse response =alipayClient.execute(request);
            String code = response.getCode();
            System.out.println("code:"+code);
            System.out.println(response.getBody());
            if (code.equals("10000")){
                map.put("out_trade_no",out_trade_no);
                map.put("tradestatus",response.getTradeStatus());
                map.put("trade_no",response.getTradeNo());
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return map;
    }
}
